// var coordtransform = require("coordtransform");
const gcoord = require('gcoord');
var fs = require("fs")

// fData = fs.readFile(__dirname + "/public/data/zhengzhou_raw_2_9.json", function(err, data){
//     if (err){
//         return console.log("error in reading file!");
//     }
//     var jsData = JSON.parse(data.toString());
//     var items = [];
//     var items_p = [];
//     for (val in jsData["郑州市"]){
//         for (var i = 0; i < jsData["郑州市"][val].length; i++){
//             items.push(jsData["郑州市"][val][i]);
//         }
//     }
//     for (var i = 0; i < items.length; i++){
//         // var gc02 = coordtransform.wgs84togcj02(items[i].lng, items[i].lat);
//         // var bd09 = coordtransform.gcj02tobd09(gc02[0], gc02[1]);
//         var bd09 = gcoord.transform([items[i].lng, items[i].lat], gcoord.GCJ02, gcoord.BD09);
//         items_p.push({
//             lng : bd09[0],
//             lat : bd09[1],
//             count : 30,
//             community : items[i].community,
//             address : items[i].full_address,
//             district : items[i].district
//         });
//     }
//     fs.writeFile(__dirname + "/public/data/zhengzhou_2_9.json", JSON.stringify(items_p), function(err, result){
//         if (err){
//             return console.log("error in writing file!");
//         }
//     });
// });

fData = fs.readFile(__dirname + "/public/data/china_raw_2_10.json", function(err, data){
    if (err){
        return console.log("error in reading file!");
    }
    var jsData = JSON.parse(data.toString());
    var items = jsData.data;
    var items_p = [];
    for (var i = 0; i < items.length; i++){
        // var gc02 = coordtransform.wgs84togcj02(items[i].lng, items[i].lat);
        // var bd09 = coordtransform.gcj02tobd09(gc02[0], gc02[1]);
        console.log(i);
        if(items[i].longitude == undefined || items[i].longitude == null || items[i].latitude == undefined || items[i].latitude == null){
            continue;
        }
        var bd09 = gcoord.transform([items[i].longitude, items[i].latitude], gcoord.GCJ02, gcoord.BD09);
        item_n = {
            lng : bd09[0],
            lat : bd09[1],
            province : items[i].province,
            city : items[i].city,
            count : 1,
            district : items[i].district,
            fullAddress : items[i].address
        }
        var address = item_n.fullAddress.replace(item_n.province, "");
        var address = address.replace(item_n.city, "");
        var address = address.replace(item_n.district, "");
        if(items[i].count > 0){
            item_n.count = items[i].count;
        }
        item_n.address = address;
        items_p.push(item_n);
    }
    fs.writeFile(__dirname + "/public/data/china_2_10.json", JSON.stringify(items_p), function(err, result){
        if (err){
            return console.log("error in writing file!");
        }
    });
});
